package cn.kust.serviceinterestpoint.controller;


import cn.kust.microtourismcommon.pojo.dto.ResponseResult;
import cn.kust.serviceinterestpoint.entity.InterestInterest;
import cn.kust.serviceinterestpoint.service.InterestInterestService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author microTourism
 * @since 2022-07-25
 */
@RestController
@RequestMapping("/serviceinterestpoint/interest-interest")
@Api(tags = "兴趣点模块")
public class InterestInterestController {

    //把service注入
    @Autowired
    private InterestInterestService interestInterestService;

    //1查询兴趣点表所有的数据
    //rest风格
    @ApiOperation(value = "所有兴趣点列表")
    @GetMapping("findAll")
    public ResponseResult<List> findAllInterest() {
        //调用service的方法实现查询所有的操作
        List<InterestInterest> list = interestInterestService.list(null);
        return ResponseResult.success(list);
    }
    //2根据ID删除兴趣点
    @GetMapping("/delete/{id}")
    @ApiOperation("根据id删除兴趣点")
    @ApiImplicitParam(value = "删除兴趣点的id",name="id",required = true, example = "1")
    public ResponseResult removeInterestById(@PathVariable Long id){
        boolean flag = interestInterestService.removeById(id);
        if (flag) {
            return ResponseResult.success("兴趣点已删除");
        } else {
            return ResponseResult.fail(404,"删除兴趣点失败");//失效
        }
    }
    //3根据兴趣点Id进行查询
    @ApiOperation("根据兴趣点Id进行查询")
    @GetMapping("getInterest/{id}")
    public ResponseResult getInterestById(@ApiParam(name = "id", value = "兴趣点id", required = true) @PathVariable long id) {
        InterestInterest byId = interestInterestService.getById(id);
        return ResponseResult.success(byId);
    }

    //4添加兴趣点接口的方法
    @ApiOperation("添加兴趣点")
    @PostMapping("addInterest")
    public ResponseResult addInterest(@RequestBody InterestInterest interestInterest) {
        boolean save = interestInterestService.save(interestInterest);
        if (save) {
            return ResponseResult.success();
        } else {
            return ResponseResult.fail(404, "兴趣点已经存在请重新添加");
        }
    }
    //5兴趣点修改功能（会覆盖原有数据）
    @ApiOperation("兴趣点修改功能")
    @PostMapping("updateInterest")
    public ResponseResult updateInterest(@RequestBody InterestInterest interestInterest) {
        boolean update = interestInterestService.updateById(interestInterest);
        if (update) {
            return ResponseResult.success();
        } else {
            return ResponseResult.fail(404, "修改失败，请重新修改");
        }
    }

    //6 分页查询兴趣点的方法
    //current当前页
    //size每页的记录数
    @ApiOperation(value = "分页查询兴趣点")//（从current第1页开始）
    @GetMapping("pageInterest/{current}/{size}")
    public ResponseResult pageList(@ApiParam(name = "current", value = "当前页", required = true) @PathVariable long current,
                                   @ApiParam(name = "size", value = "每页的记录数", required = true) @PathVariable long size) {
        //创建page对象
        Page<InterestInterest> pageInterest = new Page<>(current, size);
        //调用方法
        //自动调用方法把pageTeacher里的所有的数据封装到里面 //从数据库拿到数据
        interestInterestService.page(pageInterest, null);
        long total = pageInterest.getTotal();//总的页数
        List<InterestInterest> records = pageInterest.getRecords();//得到当前页码的数据list集合

        HashMap<String, Object> map = new HashMap<>();
        map.put("total", total);
        map.put("records", records);
        return ResponseResult.success(map);
    }


//    //7多条件组合查询带分页的方法
//    @ApiOperation(value = "条件查询分页方法")
//    @PostMapping("pageInterestCondition/{current}/{limit}")
//    public ResponseResult pageTeacherCondition(@ApiParam(name = "current", value = "当前页", required = true) @PathVariable long current,
//                                               @ApiParam(name = "limit", value = "每页的记录数", required = true) @PathVariable long limit,
//                                               @ApiParam(name = "InterestQuery", value = "查询条件", required = true) @RequestBody(required = false) interestQuery interestQuery) {
//        //创建page对象
//        Page<InterestInterest> pageCondition = new Page<>(current, limit);
//        //构建条件
//        QueryWrapper<InterestInterest> wrapper = new QueryWrapper<>();
//        //多条件组合查询
//        //mybatis 学过  动态sql
//        String city = interestQuery.getCity();
//        //判断条件值是否为空，如果不为空拼接条件
//        if (!StringUtils.isEmpty(city)) {
//            //构建条件
//            wrapper.like("city", city);
//        }
//        wrapper.orderByDesc("gmt_create");
//        //调用方法实现条件查询分页
//        interestInterestService.page(pageCondition, wrapper);
//
//        long total = pageCondition.getTotal();//获取总记录数
//        List<InterestInterest> records = pageCondition.getRecords();//获取分页后的list集合
//        HashMap<String, Object> map = new HashMap<>();
//        map.put("total", total);
//        map.put("rows", records);
//
//        return ResponseResult.success(map);
//    }


    //根据城市查询兴趣点列表
    @ApiOperation("根据兴趣点城市进行查询")
    @GetMapping("getInterestByCity")
    @ApiImplicitParam(value = "城市",name="city", required = true,example = "昆明")
    public ResponseResult getInterestByCity(String city) {
        List<InterestInterest> byCity = interestInterestService.getAllInterestsByCity(city);
        return ResponseResult.success(byCity);
    }

    //查询兴趣点的所有城市
    @ApiOperation("查询兴趣点的所有城市")
    @GetMapping("findAllCity")
    public ResponseResult findAllCity(){
        List<String> cities = interestInterestService.findAllCities();
        return ResponseResult.success(cities);
    }




}

